package com.urbansquall.ginger
{
	import flash.display.BitmapData;
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	
	/**
	 * 
	 * @author Colbycheeze
	 * @description An animation file used by AnimationPlayer. 
	 * 
	 */	
	public class Animation extends EventDispatcher
	{
		internal var m_degToRotBy:Number;
		internal var m_framesOfRotation:int;
		internal var m_frames:Array;
		internal var m_frameMap:Dictionary;
		internal var m_fps:int;
		private var m_isLooping:Boolean;
		private var m_totalFrames:int;
		
		/**
		 * 
		 * @param a_fps The default fps of this particular animation
		 * @param a_numRotations The number of precached rotations. This cannot be changed at a later time.
		 * 
		 */		
		public function Animation( a_fps:int, a_numRotations:int = 1 )
		{
			m_frames = [];
			m_frameMap = new Dictionary();
			m_fps = a_fps;
			m_isLooping = false;
			
			m_framesOfRotation = a_numRotations;
			if( m_framesOfRotation < 1 )
			{
				m_framesOfRotation = 1;
			}
			
			m_degToRotBy = 360 / m_framesOfRotation;
		}
		
		/**
		 * Adds a new frame to this current animation 
		 * @param a_numFrames The number of frames on the animation to play this image. ( think of frames on a movieclip ) 
		 * @param a_bitmapData The BitmapData to pull the image from
		 * @param a_frameLocation A rectangle definining the position of the image on the BitmapData to pull from
		 * @param a_offsetX The X offset from ( 0, 0 ). (Think registration point)
		 * @param a_offsetY The Y offset from ( 0, 0 ). (Think registration point)
		 * @param a_frameID An ID to reference this frame by. Can be left as null if not needed.
		 * @return The newly created BitmapData
		 * 
		 */		
		public function addFrame( a_numFrames: int, a_bitmapData: BitmapData,
			a_frameLocation: Rectangle, a_offsetX : Number = 0, a_offsetY : Number = 0, a_frameID: String = null ) : BitmapData
		{
			if( a_frameID == "" )
			{
				a_frameID = null;
			}
			
			var bmpData:BitmapData = new BitmapData( a_frameLocation.width, a_frameLocation.height, true, 0x00000000 );
			bmpData.copyPixels( a_bitmapData, a_frameLocation, new Point() );
			
			m_frames.push( new Frame( a_numFrames, bmpData, m_framesOfRotation, a_frameID, a_offsetX, a_offsetY ) );
			if( a_frameID != null )
			{
				m_frameMap[ a_frameID ] = m_totalFrames;
			}
			m_totalFrames++;
			
			return m_frames[ m_frames.length - 1 ].bitmapData[ 0 ];
		}
		
		public function changeFrameID( a_frameIndex : int, a_id : String ) : void
		{
			var frame : Frame = m_frames[ a_frameIndex ];
			frame.changeID( a_id );
		}
		
		public function changeFrameCount( a_frameIndex : int, a_newFrameCount : int ) : void
		{
			var frame : Frame = m_frames[ a_frameIndex ];
			frame.numFrames = a_newFrameCount;
		}
		
		public function get isLooping():Boolean
		{
			return m_isLooping;
		}
		/**
		 * Set this animation to loop over and over ( or not ) 
		 * @param a_value
		 * 
		 */		
		public function set isLooping( a_value:Boolean ): void
		{
			m_isLooping = a_value;
		}
		
		public function reverseFrames() : void
		{
			m_frames.reverse();
		}
		
		
		/*
		public function changeAllOffsets( a_offsetX: int, a_offsetY: int ) : void
		{
			for( var i : int = 0; i < m_frames.length; i++ )
			{
				var frame : Frame = m_frames[ i ];
				frame.offsetX = a_offsetX;
				frame.offsetY = a_offsetY;
			}
		}
		*/
		
	}
}
	